﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOHVote.BLL;
using System.Data;

namespace BOH.DataManagement
{
    public partial class Manageswitch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadApplyStyle();
                LoadAllowUploadPPT();
                LoadAllowModifyApplyer();
                CurrentApplyStyle();
                LoadSwitchSubject();
            }
        }

        public void LoadSwitchSubject()
        {
            DataSet dst = Subject.getAllASubject();
            //DataSet dst = SysConfigClass.getSwitchSubjects();
            this.InterviewQuestionsList.DataSource = dst;
            InterviewQuestionsList.DataBind();
        }

        public void LoadAllowUploadPPT()
        {
            string strValue = SysConfigClass.AllowUploadPPT();
            this.drpAllowUploadPPT.SelectedValue = strValue;
        }

        public void LoadApplyStyle()
        {
            DataSet dst = DictManageClass.LoadDataDictByName("ApplyStyle");
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["dict_display"].ToString(),dr["dict_value"].ToString());
                drpApplyStyle.Items.Add(li);
            }
        }

        public void CurrentApplyStyle()
        {
            string strValue = SysConfigClass.getApplyStyle();
            drpApplyStyle.SelectedValue = strValue;
        }

        protected void butApplyStyle_Click(object sender, EventArgs e)
        {
            string strCurrentApplyStyle = drpApplyStyle.SelectedValue;
            SysConfigClass.UpdateApplyStyle(strCurrentApplyStyle);
        }

        protected void butSavePPT_Click(object sender, EventArgs e)
        {
            string isAllowUploadPPT = drpAllowUploadPPT.SelectedValue;

            if (SysConfigClass.hasDataAllowUploadPPT())
            {
                SysConfigClass.UpdateAllowUploadPPT(isAllowUploadPPT);
            }
            else
            {
                SysConfigClass.InsertAllowUploadPPT(isAllowUploadPPT);
            }
        }

        public void LoadAllowModifyApplyer()
        {
            string strValue = SysConfigClass.AllowModifyApplyer();
            this.drpAllowModify.SelectedValue = strValue;
        }

        protected void butAllowModfiy_Click(object sender, EventArgs e)
        {
            string isAllowModifyApplyer = drpAllowModify.SelectedValue;

            if (SysConfigClass.hasDataModifyApplyer())
            {
                SysConfigClass.UpdateAllowModifyApplyer(isAllowModifyApplyer);
            }
            else
            {
                SysConfigClass.InsertAllowModifyApplyer(isAllowModifyApplyer);
            }
        }

        protected void butQuestionStats_Click(object sender, EventArgs e)
        {
            InterviewClass.UpdateAllStatusQuestions(questionStats.SelectedValue);
        }

        protected void InterviewQuestionsList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "save")
            {
                GridViewRow gvr = InterviewQuestionsList.Rows[Convert.ToInt32(e.CommandArgument)];
                string subjectID = ((HiddenField)gvr.FindControl("hidSubjectID")).Value;
                string IsOpen = ((DropDownList)gvr.FindControl("drpIsOpen")).SelectedValue;
                string hasData = SysConfigClass.getSwitchSubject(subjectID);
                if (!String.IsNullOrEmpty(hasData))
                {
                    SysConfigClass.UpdateSwitchSubject(subjectID, IsOpen);
                }
                else
                {
                    SysConfigClass.InsertSwitchSubject(subjectID, IsOpen);
                }

                LoadSwitchSubject();
            }
        }

        protected void InterviewQuestionsList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string subjectID = ((HiddenField)e.Row.FindControl("hidSubjectID")).Value;
                string DBIsOpen = SysConfigClass.getSwitchSubject(subjectID);
                DropDownList drIsOpen = ((DropDownList)e.Row.FindControl("drpIsOpen"));
                if (drIsOpen != null)
                {
                    drIsOpen.SelectedValue = DBIsOpen; 
                }
            }
        }
        
        private void SetOpenStatus(string IsOpen)
        {
            DataSet dst = Subject.getAllASubject();
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                string Open = SysConfigClass.getSwitchSubject(dr["subjectID"].ToString());
                if (!String.IsNullOrEmpty(Open))
                    SysConfigClass.UpdateSwitchSubject(dr["subjectID"].ToString(), IsOpen);
                else
                    SysConfigClass.InsertSwitchSubject(dr["subjectID"].ToString(), IsOpen);
            }
            LoadSwitchSubject();
        }

        protected void butOpenAll_Click(object sender, EventArgs e)
        {
            SetOpenStatus("1");

        }

        protected void butCloseAll_Click(object sender, EventArgs e)
        {
            SetOpenStatus("0");
        }

    }
}
